package util;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Map;

import org.junit.Assert;
import org.junit.Test;

import play.Play;
import play.mvc.Http.Cookie;
import play.mvc.Http.Response;

/**
 * @author oleksandr.bezhan
 * 
 *         Apr 29, 2012
 */
public class FunctionalTestHelper extends play.test.FunctionalTest {
    public static final String VALIDATION_COOKIE = "PLAY_ERRORS";

    public static void assertIsOk(Response response) {
        assertEquals((int) response.status, 200);
    }

    public static void assertValid(Response response) {
        Map<String, Cookie> cookies = response.cookies;
        Cookie cookie = cookies.get(FunctionalTestHelper.VALIDATION_COOKIE);
        try {
            String validationErrors = URLDecoder.decode(cookie.value, Play.defaultWebEncoding).trim();
            Assert.assertTrue("Validation errors found: " + validationErrors, validationErrors.isEmpty());
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    protected File getTestImage() {
        File image = new File(Play.applicationPath + File.separator + "test" + File.separator + "kartofel-thumb.jpg");
        return image;
    }

    /**
     * This is dummy test to run this class as test successfully
     */
    @Test
    public void testOk() {
        assertTrue(true);
    }
}